package org.rooster.genetic;

import org.jgap.FitnessFunction;
import org.jgap.IChromosome;
import org.rooster.chromosomeevaluator.RuleEvaluator;
import org.rooster.chromosomeevaluator.common.ScheduleMetrix;

/**
 * Created by IntelliJ IDEA.
 * User: manorama
 * Date: 6/3/12
 * Time: 6:40 PM
 * To change this template use File | Settings | File Templates.
 */
public class JGAPFitnessFunctionImplementation extends FitnessFunction {

    GenericRule[] rulesArray;

    public JGAPFitnessFunctionImplementation(RuleEvaluator ruleEvaluator) {
        rulesArray = ruleEvaluator.getRulesArray();
    }

    /**
     * Determine the fitness of the given Chromosome instance. The higher the
     * return value, the more fit the instance. This method should always
     * return the same fitness value for two equivalent Chromosome instances.
     *
     * @param chromosome: The Chromosome instance to evaluate.
     *
     * @return
     */

    /**
     * Get all the rules one by one and calculate the fitness value of the chromosome for the rules
     * Then multiply it by the weight given to the rule
     * Then multiply the fitness values
     * @param chromosome
     * @return
     */


    @Override
    protected double evaluate( final IChromosome chromosome ){
        ScheduleMetrix.getScheduleMetrixInstance().fillMetrix(chromosome);
        double fitness = 1.0;
        for(int i=0; i<rulesArray.length; i++) {
            fitness *= rulesArray[i].fitnessValueForRule(chromosome);
        }
        return fitness;
    }

}

